package com.walker.kuyun.ui.fragment

import android.animation.ObjectAnimator
import android.app.Activity
import android.graphics.Rect
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.ViewStub
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.walker.kuyun.R
import com.walker.kuyun.bean.VideoCoverBean
import com.walker.kuyun.config.Const.ViewHolderTypeInt
import com.walker.kuyun.databinding.FragmentEverydayTmBinding
import com.walker.kuyun.util.GridList1ViewHolder
import com.walker.kuyun.util.ViewHolderUtil.Companion.getViewHolder
import com.walker.kuyun.util.eventbus.EventBusSubscriber
import com.walker.kuyun.util.eventbus.MessageEvent
import com.walker.kuyun.util.eventbus.RefreshEvent
import com.walker.kuyun.ui.adapter.TMShowAdapter
import com.walker.kuyun.ui.component.WrapLinearLayoutManager
import com.walker.kuyun.viewmodel.EverydayViewModel
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


class EverydayTMFragment : BaseFragment<FragmentEverydayTmBinding>(), EventBusSubscriber {
    private lateinit var viewModel: EverydayViewModel
    private lateinit var adapter: Vp2Adapter
    private var offscreenPageLimit = 1
    private var selectedTabIndex = -1
    private var lastRefreshTime: Long = System.currentTimeMillis()

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        viewModel = ViewModelProvider(this).get(EverydayViewModel::class.java)
        return super.onCreateView(inflater, container, savedInstanceState)
    }

    override fun getBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FragmentEverydayTmBinding =
        FragmentEverydayTmBinding.inflate(inflater, container, false)

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        mBinding.run {
            vp2EverydayAnimeFragment.setOffscreenPageLimit(offscreenPageLimit)
            srlEverydayAnimeFragment.setColorSchemeResources(R.color.main_color)
            srlEverydayAnimeFragment.setOnRefreshListener { refresh() }

            tlEverydayAnimeFragment.addOnTabSelectedListener(object :
                TabLayout.OnTabSelectedListener {
                override fun onTabSelected(tab: TabLayout.Tab) {
                    selectedTabIndex = tab.position
                }

                override fun onTabUnselected(tab: TabLayout.Tab) {
                }

                override fun onTabReselected(tab: TabLayout.Tab) {
                }

            })
        }

        viewModel.mldHeader.observe(viewLifecycleOwner, Observer {
            mBinding.tvEverydayAnimeFragmentTitle.text = it.title
        })

        viewModel.mldEverydayAnimeList.observe(viewLifecycleOwner, Observer {
            mBinding.srlEverydayAnimeFragment.isRefreshing = false
            if (this::adapter.isInitialized) adapter.notifyDataSetChanged()

            if (it) {
                val selectedTabIndex = this.selectedTabIndex
                activity?.let { it1 ->
                    //先隐藏
                    ObjectAnimator.ofFloat(mBinding.llEverydayAnimeFragment, "alpha", 1f, 0f)
                        .setDuration(270).start()
                    //添加rv
                    if (!this::adapter.isInitialized) {
                        adapter = Vp2Adapter(it1, viewModel.everydayVideoList)
                        mBinding.vp2EverydayAnimeFragment.setAdapter(adapter)
                    }

                    val tabLayoutMediator = TabLayoutMediator(
                        mBinding.tlEverydayAnimeFragment,
                        mBinding.vp2EverydayAnimeFragment.getViewPager()
                    ) { tab, position ->
                        tab.text = viewModel.tabList[position].title
                    }
                    tabLayoutMediator.attach()

                    val tabCount = mBinding.tlEverydayAnimeFragment.tabCount
                    if (selectedTabIndex != -1 && selectedTabIndex < tabCount)
                        mBinding.vp2EverydayAnimeFragment.setCurrentItem(selectedTabIndex, false)
                    else if (selectedTabIndex == -1 && viewModel.selectedTabIndex < tabCount)
                        mBinding.vp2EverydayAnimeFragment.setCurrentItem(
                            viewModel.selectedTabIndex, false
                        )

                    //设置完数据后显示，避免闪烁
                    ObjectAnimator.ofFloat(mBinding.llEverydayAnimeFragment, "alpha", 0f, 1f)
                        .setDuration(270).start()
                }

                hideLoadFailedTip()
            } else {
                showLoadFailedTip(
                    getString(R.string.load_data_failed_click_to_retry),
                    View.OnClickListener {
                        viewModel.getEverydayAnimeData()
                        hideLoadFailedTip()
                    })
            }
        })

        mBinding.srlEverydayAnimeFragment.isRefreshing = true
        viewModel.getEverydayAnimeData()
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    override fun onMessageEvent(event: MessageEvent) {
        when (event) {
            is RefreshEvent -> {
                refresh()
            }
        }
    }

    private fun refresh() {
        //避免刷新间隔太短
        if (System.currentTimeMillis() - lastRefreshTime > 500) {
            mBinding.srlEverydayAnimeFragment.isRefreshing = true
            lastRefreshTime = System.currentTimeMillis()
            viewModel.getEverydayAnimeData()
        } else {
            mBinding.srlEverydayAnimeFragment.isRefreshing = false
        }
    }

    override fun getLoadFailedTipView(): ViewStub? = mBinding.layoutEverydayAnimeFragmentLoadFailed

    class Vp2Adapter//默认初始化全为false
        (
        private var activity: Activity,
        private var list: List<List<VideoCoverBean>>,
        private var showRankNumber: BooleanArray = BooleanArray(0)
    ) :
        RecyclerView.Adapter<RecyclerView.ViewHolder>() {

        //必须四个参数都不是-1才生效
        var childPadding = Rect(-1, -1, -1, -1)

        override fun getItemViewType(position: Int): Int = ViewHolderTypeInt.GRID_LIST_1

        override fun onCreateViewHolder(
            parent: ViewGroup,
            viewType: Int
        ): RecyclerView.ViewHolder {
            val viewHolder = getViewHolder(parent, viewType)
            //vp2的item必须是MATCH_PARENT的
            val layoutParams = viewHolder.itemView.layoutParams
            layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT
            layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT
            viewHolder.itemView.layoutParams = layoutParams
            return viewHolder
        }

        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            val item = list[position]
            when (holder) {
                is GridList1ViewHolder -> {
                    val rvLayoutParams = holder.rvGridRecyclerView1.layoutParams
                    rvLayoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT
                    holder.rvGridRecyclerView1.layoutManager = WrapLinearLayoutManager(activity)
                    holder.rvGridRecyclerView1.layoutParams = rvLayoutParams
                    holder.rvGridRecyclerView1.isNestedScrollingEnabled = true
                    val adapter = TMShowAdapter.GridList1Adapter(activity, item)
                    adapter.padding = childPadding
                    if (showRankNumber.isNotEmpty() && showRankNumber[position])
                        adapter.showRankNumber = true
                    holder.rvGridRecyclerView1.adapter = adapter
                    adapter.notifyDataSetChanged()
                }
            }
        }

        override fun getItemCount(): Int = list.size
    }

    companion object {
        const val TAG = "EverydayAnimeFragment"
    }
}
